﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CApSLPSetLocationsPropertiesFilterUtils
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

internal class CApSLPSetLocationsPropertiesFilterUtils
{
  private CApSLPSetLocationsPropertiesFilterUtilsCreationData _data;
  private string _selectedLocationType = string.Empty;
  private string _selectedMotionType = string.Empty;
  private string _selectedProcessType = string.Empty;
  private const string _PARAM_MOTION_TYPE = "RRS_MOTION_TYPE";
  private const string _PARAM_PROCESS_TYPE = "ProcessType";

  public CApSLPSetLocationsPropertiesFilterUtils(
    CApSLPSetLocationsPropertiesFilterUtilsCreationData creationData)
  {
    this._data = creationData;
    this.FillLocationTypesFilter();
    this.FillMotionTypesFilter();
    this.FillProcessTypesFilter();
    this._selectedLocationType = StringTable.ResourceManager.GetString("SLP_All");
    this._selectedMotionType = StringTable.ResourceManager.GetString("SLP_All");
    this._selectedProcessType = StringTable.ResourceManager.GetString("SLP_All");
    this.SelectLocationItem(this._data.LocationsFilter.DropDownMenu.MenuItems[0]);
    this.SelectMotionItem(this._data.MotionFilter.DropDownMenu.MenuItems[0]);
    this.SelectProcessItem(this._data.ProcessFilter.DropDownMenu.MenuItems[0]);
    this.ExecuteFilters();
  }

  private void FillLocationTypesFilter()
  {
    if (this._data.LocationsFilter.Style != ToolBarButtonStyle.DropDownButton)
      return;
    MenuItem[] menuItems;
    if (TxApplication.PlatformType == 3)
      menuItems = new MenuItem[7]
      {
        new MenuItem(StringTable.SLP_All, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Via, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Generic, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Seam, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_First_Seam, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Last_Seam, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Seam_Via, new EventHandler(this.LocationFilter_Click))
      };
    else
      menuItems = new MenuItem[8]
      {
        new MenuItem(StringTable.SLP_All, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Via, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Generic, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Weld, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Seam, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_First_Seam, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Last_Seam, new EventHandler(this.LocationFilter_Click)),
        new MenuItem(StringTable.SLP_LocType_Seam_Via, new EventHandler(this.LocationFilter_Click))
      };
    foreach (Menu menu in menuItems)
      menu.Tag = (object) "tbbLocationFilter";
    this._data.LocationsFilter.DropDownMenu = (Menu) new ContextMenu(menuItems);
  }

  private void FillMotionTypesFilter()
  {
    if (this._data.MotionFilter.Style != ToolBarButtonStyle.DropDownButton)
      return;
    MenuItem[] menuItems = new MenuItem[4]
    {
      new MenuItem(StringTable.ResourceManager.GetString("SLP_All"), new EventHandler(this.LocationFilter_Click)),
      new MenuItem(StringTable.ResourceManager.GetString("SLP_MotionType_Joint"), new EventHandler(this.LocationFilter_Click)),
      new MenuItem(StringTable.ResourceManager.GetString("SLP_MotionType_Linear"), new EventHandler(this.LocationFilter_Click)),
      new MenuItem(StringTable.ResourceManager.GetString("SLP_MotionType_Circular"), new EventHandler(this.LocationFilter_Click))
    };
    menuItems[0].Tag = menuItems[1].Tag = menuItems[2].Tag = menuItems[3].Tag = (object) "tbbMotionTypeFilter";
    this._data.MotionFilter.DropDownMenu = (Menu) new ContextMenu(menuItems);
  }

  private void FillProcessTypesFilter()
  {
    ITxRobotController commonController = (ITxRobotController) null;
    List<MenuItem> menuItemList = new List<MenuItem>();
    MenuItem menuItem1 = new MenuItem(StringTable.ResourceManager.GetString("SLP_All"), new EventHandler(this.LocationFilter_Click));
    menuItem1.Tag = (object) "tbbProcessTypeFilter";
    menuItemList.Add(menuItem1);
    if (CApSLPSetLocationsPropertiesLogic.GetCommonController(out commonController) && commonController != null && commonController.Name != "default")
    {
      Hashtable hashtable = (Hashtable) null;
      try
      {
        hashtable = new TxOlpMotionCommandServices(CApSLPSetLocationsPropertiesLogic.GetRobot()).GetProcessTypesMap();
      }
      catch
      {
      }
      if (hashtable != null)
      {
        foreach (DictionaryEntry dictionaryEntry in hashtable)
        {
          foreach (string text in dictionaryEntry.Value as ArrayList)
          {
            if (!string.IsNullOrEmpty(text))
            {
              MenuItem menuItem2 = new MenuItem(text, new EventHandler(this.LocationFilter_Click));
              menuItem2.Tag = (object) "tbbProcessTypeFilter";
              menuItemList.Add(menuItem2);
            }
          }
        }
      }
    }
    this._data.ProcessFilter.DropDownMenu = (Menu) new ContextMenu(menuItemList.ToArray());
  }

  private void LocationFilter_Click(object sender, EventArgs e)
  {
    if (!(sender is MenuItem))
      return;
    switch ((sender as MenuItem).Tag as string)
    {
      case "tbbLocationFilter":
        this._selectedLocationType = (sender as MenuItem).Text;
        this.SelectLocationItem(sender as MenuItem);
        break;
      case "tbbMotionTypeFilter":
        this._selectedMotionType = (sender as MenuItem).Text;
        this.SelectMotionItem(sender as MenuItem);
        break;
      case "tbbProcessTypeFilter":
        this._selectedProcessType = (sender as MenuItem).Text;
        this.SelectProcessItem(sender as MenuItem);
        break;
    }
    this.ExecuteFilters();
  }

  public void ExecuteFilters()
  {
    TxPathEditorToDotNetGap editorToDotNetGap = new TxPathEditorToDotNetGap();
    editorToDotNetGap.StartCacheMode();
    try
    {
      this._data.FilterLocationsReference(this.GenerateFilteredList());
      this._data.UpdateFiltersCaptionReference(this.UpdateActiveFiltersCaption());
    }
    finally
    {
      editorToDotNetGap.EndCacheMode();
      GC.KeepAlive((object) editorToDotNetGap);
    }
  }

  public TxObjectList GenerateFilteredList()
  {
    Type[] locationTypes = (Type[]) null;
    if (this._selectedLocationType == StringTable.ResourceManager.GetString("SLP_All"))
      locationTypes = new Type[4]
      {
        typeof (TxRoboticViaLocationOperation),
        typeof (TxWeldLocationOperation),
        typeof (TxRoboticSeamLocationOperation),
        typeof (TxGenericRoboticLocationOperation)
      };
    else if (this._selectedLocationType == StringTable.SLP_LocType_Via)
      locationTypes = new Type[1]
      {
        typeof (TxRoboticViaLocationOperation)
      };
    else if (this._selectedLocationType == StringTable.SLP_LocType_Weld)
      locationTypes = new Type[1]
      {
        typeof (TxWeldLocationOperation)
      };
    else if (this._selectedLocationType == StringTable.SLP_LocType_Seam)
      locationTypes = new Type[1]
      {
        typeof (TxRoboticSeamLocationOperation)
      };
    else if (this._selectedLocationType == StringTable.SLP_LocType_First_Seam)
      locationTypes = new Type[1]
      {
        typeof (TxRoboticSeamLocationOperation)
      };
    else if (this._selectedLocationType == StringTable.SLP_LocType_Last_Seam)
      locationTypes = new Type[1]
      {
        typeof (TxRoboticSeamLocationOperation)
      };
    else if (this._selectedLocationType == StringTable.SLP_LocType_Seam_Via)
      locationTypes = new Type[1]
      {
        typeof (TxRoboticSeamLocationOperation)
      };
    else if (this._selectedLocationType == StringTable.SLP_LocType_Generic)
      locationTypes = new Type[1]
      {
        typeof (TxGenericRoboticLocationOperation)
      };
    TxObjectList preFilterList = this.FilterLocations(locationTypes);
    if (this._selectedLocationType == StringTable.ResourceManager.GetString("SLP_LocType_First_Seam"))
      preFilterList = this.FilterSeamFirstLocation(preFilterList);
    else if (this._selectedLocationType == StringTable.ResourceManager.GetString("SLP_LocType_Last_Seam"))
      preFilterList = this.FilterSeamLastLocation(preFilterList);
    else if (this._selectedLocationType == StringTable.ResourceManager.GetString("SLP_LocType_Seam_Via"))
      preFilterList = this.FilterSeamViaLocation(preFilterList);
    if (!(this._selectedMotionType == StringTable.ResourceManager.GetString("SLP_All")))
    {
      if (this._selectedMotionType == StringTable.ResourceManager.GetString("SLP_MotionType_Circular"))
        preFilterList = this.FilterMotionType((TxRRSMotionType) 4, preFilterList);
      else if (this._selectedMotionType == StringTable.ResourceManager.GetString("SLP_MotionType_Joint"))
        preFilterList = this.FilterMotionType((TxRRSMotionType) 1, preFilterList);
      else if (this._selectedMotionType == StringTable.ResourceManager.GetString("SLP_MotionType_Linear"))
        preFilterList = this.FilterMotionType((TxRRSMotionType) 2, preFilterList);
    }
    if (!(this._selectedProcessType == StringTable.ResourceManager.GetString("SLP_All")))
      preFilterList = this.FilterProcessType(preFilterList);
    return preFilterList;
  }

  public void Reset()
  {
    this._selectedLocationType = StringTable.ResourceManager.GetString("SLP_All");
    this._selectedMotionType = StringTable.ResourceManager.GetString("SLP_All");
    this._selectedProcessType = StringTable.ResourceManager.GetString("SLP_All");
    this.SelectLocationItem(this._data.LocationsFilter.DropDownMenu.MenuItems[0]);
    this.SelectMotionItem(this._data.MotionFilter.DropDownMenu.MenuItems[0]);
    this.SelectProcessItem(this._data.ProcessFilter.DropDownMenu.MenuItems[0]);
    this.ExecuteFilters();
  }

  private string UpdateActiveFiltersCaption()
  {
    string str1 = StringTable.ResourceManager.GetString("SLP_FilterTitle") + ": ";
    if (string.IsNullOrEmpty(this._selectedLocationType))
      this._selectedLocationType = StringTable.ResourceManager.GetString("SLP_All");
    if (string.IsNullOrEmpty(this._selectedMotionType))
      this._selectedMotionType = StringTable.ResourceManager.GetString("SLP_All");
    if (string.IsNullOrEmpty(this._selectedProcessType))
      this._selectedProcessType = StringTable.ResourceManager.GetString("SLP_All");
    string str2 = (!(this._selectedLocationType == StringTable.ResourceManager.GetString("SLP_All")) ? str1 + this._selectedLocationType : str1 + string.Format(StringTable.ResourceManager.GetString("SLP_LocationTypeFilterTitle"), (object) this._selectedLocationType)) + ", ";
    string str3 = (!(this._selectedMotionType == StringTable.ResourceManager.GetString("SLP_All")) ? str2 + this._selectedMotionType : str2 + string.Format(StringTable.ResourceManager.GetString("SLP_MotionTypeFilter"), (object) this._selectedMotionType)) + ", ";
    return !(this._selectedProcessType == StringTable.ResourceManager.GetString("SLP_All")) ? str3 + this._selectedProcessType : str3 + string.Format(StringTable.ResourceManager.GetString("SLP_ProcessTypeFilter"), (object) this._selectedProcessType);
  }

  private TxObjectList FilterLocations(Type[] locationTypes)
  {
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    if (locationTypes != null && locationTypes.Length != 0)
    {
      foreach (Type locationType in locationTypes)
        txTypeFilter.AddIncludedType(locationType);
    }
    return this._data.PreSelectedLocations != null ? txTypeFilter.Filter(this._data.PreSelectedLocations) : new TxObjectList();
  }

  private TxObjectList FilterSeamFirstLocation(TxObjectList preFilterList)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxRoboticLocationOperation preFilter in (Collection<ITxObject>) preFilterList)
    {
      if (preFilter is TxRoboticSeamLocationOperation && ((ITxObject) preFilter).Collection != null && ((ITxObject) preFilter).Collection is TxRoboticSeamOperation && (((ITxObject) preFilter).Collection as TxRoboticSeamOperation).GetIndexOfChild((ITxObject) preFilter) == 0)
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) preFilter);
    }
    return txObjectList;
  }

  private TxObjectList FilterSeamLastLocation(TxObjectList preFilterList)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxRoboticLocationOperation preFilter in (Collection<ITxObject>) preFilterList)
    {
      if (preFilter is TxRoboticSeamLocationOperation && ((ITxObject) preFilter).Collection != null && ((ITxObject) preFilter).Collection is TxRoboticSeamOperation && (((ITxObject) preFilter).Collection as TxRoboticSeamOperation).GetIndexOfChild((ITxObject) preFilter) == (((ITxObject) preFilter).Collection as TxRoboticSeamOperation).Count - 1)
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) preFilter);
    }
    return txObjectList;
  }

  private TxObjectList FilterSeamViaLocation(TxObjectList preFilterList)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxRoboticLocationOperation preFilter in (Collection<ITxObject>) preFilterList)
    {
      if (preFilter is TxRoboticSeamLocationOperation && ((ITxObject) preFilter).Collection != null && ((ITxObject) preFilter).Collection is TxRoboticSeamOperation && (((ITxObject) preFilter).Collection as TxRoboticSeamOperation).GetIndexOfChild((ITxObject) preFilter) != (((ITxObject) preFilter).Collection as TxRoboticSeamOperation).Count - 1 && (((ITxObject) preFilter).Collection as TxRoboticSeamOperation).GetIndexOfChild((ITxObject) preFilter) != 0)
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) preFilter);
    }
    return txObjectList;
  }

  private void SelectLocationItem(MenuItem activeItem)
  {
    foreach (MenuItem menuItem in this._data.LocationsFilter.DropDownMenu.MenuItems)
      menuItem.Checked = false;
    activeItem.Checked = true;
  }

  private TxObjectList FilterMotionType(
    TxRRSMotionType chosenMotionType,
    TxObjectList preFilterList)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxRoboticLocationOperation preFilter in (Collection<ITxObject>) preFilterList)
    {
      if (((ITxRoboticOperation) preFilter).GetParameter("RRS_MOTION_TYPE") is TxRoboticIntParam parameter && parameter.Value == chosenMotionType)
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) preFilter);
    }
    return txObjectList;
  }

  private void SelectMotionItem(MenuItem activeItem)
  {
    foreach (MenuItem menuItem in this._data.MotionFilter.DropDownMenu.MenuItems)
      menuItem.Checked = false;
    activeItem.Checked = true;
  }

  private TxObjectList FilterProcessType(TxObjectList preFilterList)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxRoboticLocationOperation preFilter in (Collection<ITxObject>) preFilterList)
    {
      TxRoboticParam parameter = ((ITxRoboticOperation) preFilter).GetParameter("ProcessType");
      if (parameter != null && parameter is TxRoboticStringParam && (parameter as TxRoboticStringParam).Value == this._selectedProcessType)
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) preFilter);
    }
    return txObjectList;
  }

  private void SelectProcessItem(MenuItem activeItem)
  {
    foreach (MenuItem menuItem in this._data.ProcessFilter.DropDownMenu.MenuItems)
      menuItem.Checked = false;
    activeItem.Checked = true;
  }
}
